/*
* SuctionController.java
* Created on  2015/7/21 10:26
* 版本       修改时间          作者      修改内容
* V1.0.1    2015/7/21       longshihui    初始版本
*
*/

package com.infitecs.eyas.oncefan;

import com.infitecs.eyas.boiler.model.AirGasParameter;
import com.infitecs.eyas.boiler.model.Efficiency;
import com.infitecs.eyas.boiler.service.AirGasParameterService;
import com.infitecs.eyas.boiler.service.EfficiencyService;
import com.infitecs.eyas.oncefan.model.OnceFanDes;
import com.infitecs.eyas.oncefan.service.OnceFanDesService;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.dao.PowerDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.power.model.Power;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.step.service.StepService;
import com.infitecs.eyas.suction.model.SuctionDes;
import com.infitecs.krill.ajax.response.AjaxResponse;
import com.infitecs.krill.ajax.response.ReturnState;
import com.infitecs.krill.exception.ValidateErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 类的描述信息
 *
 * @author longshihui
 * @version 1.0.1
 */
@Controller
@RequestMapping("/OnceFan/design")
public class OnceDesController {

    @Autowired
    private SessionService sessionService;

    @Autowired
    private OnceFanDesService onceFanDesService;

    @Autowired
    private EfficiencyService efficiencyService;

    @Autowired
    private AirGasParameterService airGasParameterService;
    @Autowired
    private StepService stepService;
    @Autowired
    private OperateFieldDao operateFieldDao;
    @Autowired
    private PowerDao powerDao;

    @RequestMapping("")
    public String getPage(HttpServletRequest request) {
        String cId = (String) request.getSession().getAttribute("caseId");
        if (cId == null) {
            return "oncefan/design";
        }
        int caseId = Integer.parseInt(cId);
        int step = stepService.getMzFxStep(caseId);
        //判断当前方案流程到哪里
        if (step < 6) {
            request.setAttribute("step", step);
            return "oncefan/design";
        }
        Power powerTitle = powerDao.getCaseById(cId);
        OperateField operateField = operateFieldDao.getOperateField(caseId);
        OnceFanDes onceFanDes = onceFanDesService.getOnceFanDes();
        request.setAttribute("powerTitle", powerTitle);
        request.setAttribute("onceFanDes", onceFanDes);
        request.setAttribute("caseId", caseId);
        request.setAttribute("operateField", operateField);
        request.setAttribute("message", request.getParameter("message"));
        return "oncefan/design";
    }

    private void getInitOnceFanDes() {
        int caseId = sessionService.getCaseId();
        AirGasParameter airGasParameter = airGasParameterService.getAirGasParameter(caseId);
        Efficiency efficiency = efficiencyService.getEfficiency(caseId);
        OnceFanDes onceFanDes = onceFanDesService.getOnceFanDes();
        if (efficiency != null) {
            if (onceFanDes.getE1() == null) {
                onceFanDes.setE1(efficiency.getE93());
                onceFanDesService.saveValue(setParamValue("E1", efficiency.getE93()));
            }
            if (onceFanDes.getE5() == null) {
                onceFanDes.setE5(efficiency.getE52());
                onceFanDesService.saveValue(setParamValue("E5", efficiency.getE52()));
            }
        }
        if (airGasParameter != null) {
            if (onceFanDes.getE4() == null) {
                onceFanDes.setE4(airGasParameter.getAirPreheaterAirLeakageCoefficient());
                onceFanDesService.saveValue(setParamValue("E4", airGasParameter.getAirPreheaterAirLeakageCoefficient()));
            }
            if (onceFanDes.getE2() == null) {
                onceFanDes.setE2(airGasParameter.getHearthAirLeakageRate());
                onceFanDesService.saveValue(setParamValue("E2", airGasParameter.getHearthAirLeakageRate()));
            }
            if (onceFanDes.getE3() == null) {
                onceFanDes.setE3(airGasParameter.getHearthOverOutAirCoefficient());
                onceFanDesService.saveValue(setParamValue("E3", airGasParameter.getHearthOverOutAirCoefficient()));
            }
            if (onceFanDes.getE6() == null) {
                onceFanDes.setE6((double) airGasParameter.getWarehouse());
                onceFanDesService.saveValue(setParamValue("E6", (double) airGasParameter.getWarehouse()));
            }
            if (onceFanDes.getE7() == null) {
                onceFanDes.setE7(airGasParameter.getOnceWindRate());
                onceFanDesService.saveValue(setParamValue("E7", airGasParameter.getOnceWindRate()));
            }
            if (onceFanDes.getE9() == null) {
                onceFanDes.setE9(airGasParameter.getInOnceWindTemperature());
                onceFanDesService.saveValue(setParamValue("E9", airGasParameter.getInOnceWindTemperature()));
            }
            if (onceFanDes.getE8() == null) {
                onceFanDes.setE8(airGasParameter.getCoalInOnceWindTemperature());
                onceFanDesService.saveValue(setParamValue("E8", airGasParameter.getCoalInOnceWindTemperature()));
            }
            if (onceFanDes.getE10() == null) {
                onceFanDes.setE10(airGasParameter.getOutOnceWindTemperature());
                onceFanDesService.saveValue(setParamValue("E10", airGasParameter.getOutOnceWindTemperature()));
            }
        }

    }

    private OnceFanDes setParamValue(String id, Double value) {
        OnceFanDes onceFanDes = new OnceFanDes();
        onceFanDes.setParamName(id);
        onceFanDes.setParamValue(value);
        return onceFanDes;
    }

    @RequestMapping(value = "/saveValue", method = RequestMethod.POST)
    @ResponseBody
    public void saveValue(OnceFanDes onceFanDes) {
        onceFanDesService.saveValue(onceFanDes);
    }

    @RequestMapping(value = "/getRecover", method = RequestMethod.GET)
    @ResponseBody
    public OnceFanDes getRecover() {
        getInitOnceFanDes();
        return onceFanDesService.getOnceFanDes();
    }

    @RequestMapping(value = "/calculateInit", method = RequestMethod.POST)
    @ResponseBody
    public void calculateInit() {
        sessionService.putOnceFanDesInit(sessionService.getCaseId());
    }

    @RequestMapping(value = "/Re-reference", method = RequestMethod.POST)
    @ResponseBody
    public OnceFanDes reReference() {
        int caseId = sessionService.getCaseId();
        AirGasParameter airGasParameter = airGasParameterService.getAirGasParameter(caseId);
        Efficiency efficiency = efficiencyService.getEfficiency(caseId);
        OnceFanDes onceFanDes = onceFanDesService.getOnceFanDes();
        if (efficiency != null) {

            onceFanDes.setE1(efficiency.getE93());
            onceFanDesService.saveValue(setParamValue("E1", efficiency.getE93()));


            onceFanDes.setE5(efficiency.getE52());
            onceFanDesService.saveValue(setParamValue("E5", efficiency.getE52()));

        }
        if (airGasParameter != null) {

            onceFanDes.setE4(airGasParameter.getAirPreheaterAirLeakageCoefficient());
            onceFanDesService.saveValue(setParamValue("E4", airGasParameter.getAirPreheaterAirLeakageCoefficient()));


            onceFanDes.setE2(airGasParameter.getHearthAirLeakageRate());
            onceFanDesService.saveValue(setParamValue("E2", airGasParameter.getHearthAirLeakageRate()));


            onceFanDes.setE3(airGasParameter.getHearthOverOutAirCoefficient());
            onceFanDesService.saveValue(setParamValue("E3", airGasParameter.getHearthOverOutAirCoefficient()));


            onceFanDes.setE6((double) airGasParameter.getWarehouse());
            onceFanDesService.saveValue(setParamValue("E6", (double) airGasParameter.getWarehouse()));


            onceFanDes.setE7(airGasParameter.getOnceWindRate());
            onceFanDesService.saveValue(setParamValue("E7", airGasParameter.getOnceWindRate()));


            onceFanDes.setE9(airGasParameter.getInOnceWindTemperature());
            onceFanDesService.saveValue(setParamValue("E9", airGasParameter.getInOnceWindTemperature()));


            onceFanDes.setE8(airGasParameter.getCoalInOnceWindTemperature());
            onceFanDesService.saveValue(setParamValue("E8", airGasParameter.getCoalInOnceWindTemperature()));


            onceFanDes.setE10(airGasParameter.getOutOnceWindTemperature());
            onceFanDesService.saveValue(setParamValue("E10", airGasParameter.getOutOnceWindTemperature()));

        }
        return onceFanDesService.getOnceFanDes();
    }

    @RequestMapping(value = "onceFanDesSave", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse onceFanDesSave(OnceFanDes onceFanDes) {
        try {
            onceFanDesService.onceFanDesSave(onceFanDes);
            return new AjaxResponse();
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof ValidateErrorException) {
                return new AjaxResponse(ReturnState.WARNING, e.getMessage());
            }
            return new AjaxResponse(ReturnState.ERROR, "请检查填写数据是否有误，操作失败");
        }
    }
}
